/*
** Copyright (c) 2008 Mobile GCalendar
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use,
** copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following
** conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
** OTHER DEALINGS IN THE SOFTWARE.
*/

package au.edu.mq.comp.itec800.mgc.lib.calendar;

import java.util.HashMap;

import au.edu.mq.comp.itec800.mgc.util.StringUtils;

/**
 * A simple color-palette management class.
 *
 * This enum class lists the colors supported by the Google Calendar as of
 * the date of production of this software, and provides various methods
 * to access them.
 *
 *
 * @author Laurent Malvert [laurent.malvert@students.mq.edu.au]
 * @author Kenfe-Mickael Laventure [kenfe-mickael.laventure@students.mq.edu.au]
 */
public enum Colors
{
  /*
   * names have been determined with the following application:
   *   http://www.colblindor.com/color-name-hue/
   */
  Color1  ("Brown"              , "#A32929"),
  Color2  ("Night Shadz"        , "#B1365F"),
  Color3  ("Palatinate Purple"  , "#7A367A"),
  Color4  ("Purple Heart"       , "#5229A3"),
  Color6  ("Venice Blue"        , "#29527A"),
  Color7  ("Endeavour"          , "#2952A3"),
  Color8  ("Elf Green"          , "#1B887A"),
  Color9  ("Sea Green"          , "#28754E"),
  Color10 ("Jewel"              , "#0D7813"),
  Color11 ("Olive"              , "#528800"),
  Color12 ("Golden Brown"       , "#88880E"),
  Color13 ("Dark Goldenrod"     , "#AB8B00"),
  Color14 ("Tawny"              , "#BE6D00"),
  Color15 ("Rust"               , "#B1440E"),
  Color16 ("Rose Taupe"         , "#865A5A"),
  Color17 ("Trendy Pink"        , "#705770"),
  Color18 ("Fiord"              , "#4E5D6C"),
  Color19 ("Kashmir Blue"       , "#5A6986"),
  Color20 ("Dark Green Copper"  , "#4A716C"),
  Color21 ("Verdigris"          , "#6E6E41"),
  Color22 ("Dark Tan"           , "#8D6F47");

  private static final HashMap<String, Colors>  reversed  = new HashMap<String, Colors>(Colors.values().length);
  private static final HashMap<String, Colors>  byName    = new HashMap<String, Colors>(Colors.values().length);

  private String								                name      = null;
  private String                                rgb       = null;


  static
  {
    for (final Colors c : Colors.values())
    {
      reversed.put(c.getRGB(), c);
      byName.put(c.getName().toLowerCase(), c);
    }
  }


  private Colors(final String name, final String rgb)
  {
    this.name = name;
    this.rgb  = rgb;
  }

  public String         getRGB()
  {
    return (rgb);
  }

  public String         getName()
  {
    return (name);
  }

  public static Colors  fromName(final String name)
  {
    final String  key = StringUtils.clean(name).toLowerCase();

    return (byName.containsKey(key) ? byName.get(key) : values()[0]);
  }

  public static Colors  fromRGB(final String rgb)
  {
    final String        key = StringUtils.clean(rgb).toLowerCase();

    return (reversed.containsKey(key) ? reversed.get(key) : values()[0]);
  }

  public static Colors  fromId(final String id)
  {
    int                 idx  = 0;

    try
    {
      idx = Integer.valueOf((id != null) ? id.trim() : "0");
    }
    catch (final NumberFormatException e)
    {
      /* do nothing */
    }
    return (fromId(idx));
  }

  public static Colors  fromId(final int id)
  {
    return (((id >= 0) && (id < values().length)) ? values()[id] : values()[0]);
  }
}
